package jp.co.hcs.attendancemanagement.dialog.logic;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import jp.co.hcs.attendancemanagement.data.dao.DepartmentDAO;
import jp.co.hcs.attendancemanagement.data.entity.Department;
import jp.co.hcs.attendancemanagement.dialog.logic.dto.DepartmentDTO;
import jp.co.hcs.attendancemanagement.logic.LogicBase;

/**
 * The Class DepartmentListLogic.
 */
public class DepartmentListLogic extends LogicBase {

  /** The dept dao. */
  private DepartmentDAO deptDao;

  /**
   * Gets the department list by division id.
   * 
   * @param divisionId the division id
   * @param deptSelected the dept selected
   * @return the department list by division id
   */
  public List<DepartmentDTO> getDepartmentListByDivisionId(String divisionId, String deptSelected) {
    List<DepartmentDTO> listDepts = new ArrayList<DepartmentDTO>();
    DepartmentDTO deptDto;

    List<Department> result = null;
    try {
      result = getDeptDao().getDepartmentListByDivisionId(divisionId);
    } catch (SQLException e) {
      return listDepts;
    }

    for (Department deptObj : result) {
      deptDto = new DepartmentDTO();
      deptDto.setDepartmentCode(deptObj.getDepartmentId());
      deptDto.setDepartmentName(deptObj.getDepartmentName());
      deptDto.setSelected(deptObj.getDepartmentId().equals(deptSelected));
      listDepts.add(deptDto);
    }
    return listDepts;
  }

  /**
   * Gets the dept dao.
   * 
   * @return the dept dao
   */
  public DepartmentDAO getDeptDao() {
    if (deptDao == null) {
      deptDao = daoFactory.getDepartmentDAO();
    }

    return deptDao;
  }

  /**
   * Sets the dept dao.
   * 
   * @param deptDao the new dept dao
   */
  public void setDeptDao(DepartmentDAO deptDao) {
    this.deptDao = deptDao;
  }

}
